package WebRoot.demo5.controller;

import org.hoyi.dishop.Hoyipage;
import org.hoyi.nosql.mongo.HoyiMongoCluster;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@RequestMode(MODE = { RequestType.GET, RequestType.POST })
public class MongodbDemo2 extends Hoyipage {

	// mongodb 存与取
	@RequestMode(MODE = { RequestType.POST, RequestType.GET })
	public void mongodbListTest() {
		DBCollection collection = HoyiMongoCluster.getDatabase("admin").getCollection("user");

		DBObject dbObject = new BasicDBObject();
		dbObject.put("age1", "30");
		collection.save(dbObject);

		DBCursor cursor = collection.find();
		this.WriteUTF8JSONDATAMSG(1, "success", cursor.toArray());
	}

	// mongodb 创建
	@RequestMode(MODE = { RequestType.POST, RequestType.GET })
	public void mongodbCreate() {
		DB db = HoyiMongoCluster.getDatabase("nadmin");
		DBCollection collection = db.getCollection("user");
		collection.insert(new BasicDBObject("key1", "value1"));
		collection.insert(new BasicDBObject("key1", "value2"));
		collection.insert(new BasicDBObject("key1", "value3"));
		DBCursor cursor = collection.find();
		this.WriteUTF8JSONDATAMSG(1, "success", cursor.toArray());
	}

//	// mongodb 删除
//	@RequestMode(MODE = { RequestType.POST, RequestType.GET })
//	public void mongodbDelete() {
//		DB db = HoyiMongoCluster.getDatabase("nadmin");
//		DBCollection collection = db.getCollection("user");
//		collection.distinct("key1");
//
//		DBCursor cursor = collection.find();
//		this.WriteUTF8JSONDATAMSG(1, "success", cursor.toArray());

		// MongoDatabase mongoDatabase = HoyiMongoCluster.getMongoClient()
		// .getDatabase(ConfigUtil.GetMongoProperties().getProperty("MONGO_DBNAME"));
		// MongoCollection<Document> collection2 = mongoDatabase.getCollection("user");
		//
		// DBCollection collection3 = db.getCollection("user");
		// collection3.find();
//
//	}

	// public static void agesOfAllWords(DBCollection dbCollection) throws Exception
	// {
	// List<Long> result = dbCollection.distinct("age");
	// System.out.println(result);
	// }
	//
	//
	// public static void namesOfAllWords(DBCollection dbCollection)throws Exception
	// {
	// List<String> result = dbCollection.distinct("name");
	// System.out.println(result);
	// }
	//
	// }

	// public void mongodbPage() {
	// try {
	//
	// MongoClient mongoClient = new MongoClient("localhost", 27017);
	// DB db = mongoClient.getDB("test");
	// DBCollection dbCollection = db.getCollection("user");
	//
	// // 查询条件
	// BasicDBObject query = new BasicDBObject("age", new BasicDBObject("$lt",
	// 100));
	//
	// DBCursor dbCursor = dbCollection.find(query);
	//
	// // 过滤条件
	// dbCursor.sort(new BasicDBObject("age", 1)); // 数据集按age字段进行正序排序.
	// dbCursor.limit(5); // 限制返回条数.
	// dbCursor.skip(5); // 跳过前面指定数量的数据
	//
	// while (dbCursor.hasNext()) {
	// DBObject dbObject = dbCursor.next();
	// System.out.println(dbObject);
	// }
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	String getpath() {
		return this.getRequest().getServletContext().getContextPath();

	}

}
